import { queryOrderInfoList, receiveOrder, queryStatisticsOrder } from '../../services/order'
import { checkIfLogin } from '../../utils/check'
import { getInfo } from '../../services/profile'
import dayjs from 'dayjs'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 'all',
    orderList:[],
    userInfo:{},
    allOrderNum:0,
    showDialogConfirm:false,
    dialogMsg:"",
    currentItem: {},
    orderId:"",
    searchText:"",
    statisticInfo:{},
    showSelectTime: false,
    currentDate: new Date().getTime(),
    selectDate:"",
    currentYear: '',
    currentMonth: ''
  },

  onLoad(options){
    // checkIfLogin()
    if(!options){
      return
    }

    let tabIndex = ""
    tabIndex = options.tabIndex
    this.setData({
      active: tabIndex ? tabIndex : 'all'
    })
    this.checkStatus(tabIndex)
    this.getUserInfo()
  },

  onShow(){
    let selectDate = dayjs().format("YYYY-MM")
    let timeArr = selectDate.split('-')
    this.setData({
      selectDate,
      currentYear: timeArr[0],
      currentMonth: timeArr[1]
    })

    if(this.data.active){
      let tabIndex = this.data.active
      this.checkStatus(tabIndex)
    }
    this.getUserInfo()
    this.getStatisticsOrder()
  },

  handleChangeTab(event){
    checkIfLogin()
    let active = event.detail
    this.setData({
      active
    })
    this.checkStatus(active)
  },

  checkStatus(tabIndex){
    let status = ""
    let selectDate = this.data.selectDate || dayjs().format("YYYY-MM")
    let data = {
      type:'TYPE_APPOINT',
      yearMonth: selectDate 
    }
    let url = "detect/order/listDetectOrder"
    if( tabIndex == 'pendingOrder' ){
      status = 1
      url = 'detect/order/list'
    } else if( tabIndex == 'toBeProcessed' ){
      status = 2
    } else if( tabIndex == 'handled' ){
      status = 3
    }

    if(status){
      data.status = status
    }
    this.getOrderList(url, data)
  },
  
  async getStatisticsOrder(){
    let res = await queryStatisticsOrder()
    console.log("res>>>", res)
    if(res.code === 200){
      this.setData({
        statisticInfo: res.data
      })
    }
  },


  async getUserInfo(){
    let userRes = await getInfo()
    console.log("userRes>>>", userRes)
    if(userRes.code === 200){
      let userInfo = userRes.user
      this.setData({
        userInfo
      })
    }
  },

  async getOrderList(url, data){
    let queryData = data
    queryData.pageSize = 1000
    queryData.pageNum = 1

    let res = await queryOrderInfoList(url, data)
    if(res.code === 200){
      let orderList = res.rows
      
      if(this.data.active == 'all'){
        let allOrderNum = res.total
        this.setData({
          allOrderNum
        })
      }
      this.setData({
        orderList
      })
    }
  },

  handleViewDetail(event){
    let currentItem = event.detail
    let id = event.detail.id
    let vehicleId = event.detail.vehicleId
    if( currentItem.status == 1 ){
      wx.navigateTo({
        url: `/pages/order-tobe/order-tobe?id=${id}`,
      })
    } else {
      // if( currentItem.status == 2 )
      wx.navigateTo({
        url: `/pages/order-handle/order-handle?id=${id}&vehicleId=${vehicleId}`,
      })
    }
  },

  handleTakeOrder(event){
    let currentItem = event.detail
    let orderId = currentItem.id
    this.setData({
      orderId,
      showDialogConfirm:true,
      dialogMsg:"是否确认接单?",
    })
  },

  async handleConfirm(){
    let confirmRes = await receiveOrder(this.data.orderId)
    if(confirmRes.code === 200){
      wx.reLaunch({
        url: `/pages/order-take-success/order-take-success?orderId=${this.data.orderId}`,
      })
    }
  },

  handSelectTime(){
    this.setData({
      selectDate: dayjs().format("YYYY-MM"),
      showSelectTime: true
    })
  },

  onInput(event){
    console.log("event>>>", event)
    let selectDate = dayjs(event.detail).format("YYYY-MM")
    this.setData({
      currentDate: event.detail,
      selectDate
    });
    console.log("selectDate>>>", selectDate)
  },

  onCancel(){
    this.setData({
      selectDate: dayjs().format("YYYY-MM"),
      showSelectTime: false
    })
  },

  onConfirm(){
    let selectDate = this.data.selectDate
    let timeArr = selectDate.split('-')
    this.setData({
      currentYear: timeArr[0],
      currentMonth: timeArr[1],
      showSelectTime: false
    })
    if(this.data.active){
      let tabIndex = this.data.active
      this.checkStatus(tabIndex)
    }
  }


})